import { getOidcUser } from '@/lib/auth';
const mgr = global.mgr;

// 判断走退出
export function judgeToLogout (outUrl, store) {
  // delCookieAndLocal();
  console.log('退出登录-------');
  // localStorage.clear();
  // store.dispatch('user/logout');
  const user = getOidcUser();
  console.log(user);
  if (user && user.id_token) {
    console.log(1);
    mgr.signoutRedirect({
      id_token_hint: user.id_token,
      extraQueryParams: {
        returnUrl: outUrl
      }
    }).then(() => {
      console.log('logouts_____________');
      if (store) {
        store.dispatch('user/logout');
      }
      mgr.removeUser();
      delCookieAndLocal();
    });
  } else {
    console.log(2);
    // delCookieAndLocal();
    window.location.href = outUrl;
  }
  // console.error(error);
  // // delCookieAndLocal();
  // window.location.href = outUrl;
  // window.location.href = outUrl;
}
// 清除所有cookie 和 localstory
export function delCookieAndLocal () {
  localStorage.clear();
  sessionStorage.clear();
  var keys = document.cookie.match(/[^ =;]+(?==)/g);
  if (keys) {
    for (var i = keys.length; i--;) {
      document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString(); // 清除当前域名下的,例如：m.ratingdog.cn
      document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString(); // 清除当前域名下的，例如 .m.ratingdog.cn
      document.cookie = keys[i] + '=0;path=/;domain=ratingdog.cn;expires=' + new Date(0).toUTCString(); // 清除一级域名下的或指定的，例如 .ratingdog.cn
    }
  }
}
// 登陆
export function loginOidc (option) {
  console.log(option);
  mgr.signinRedirect(option); // 跳转登录
}
